package git.etl.eval;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import git.bankview.sl.util.DatetimeUtil;
import git.etl.ExprParseException;
import git.etl.ExprEvalTask.EetJobD;
import git.etl.eval.IEvaluator.IFunction;
import git.etl.eval.IEvaluator.ILex;

/**
 * count factor 1 by factor (2,N); then match it to every element in base
 */
public class FuncMonth
	implements IFunction
{
	@Override
	public Map<List<String>, BigDecimal[]> perform(
			Map<List<String>, BigDecimal[]> base, ILex lex, EetJobD t,
			IEvaluator owner)
		throws Exception
	{
		String w = lex.nextElement();
		if (!"(".equals(w))
			throw new ExprParseException("'('", w);
		
		java.sql.Date dk = owner.getDateKey();
		w = lex.nextElement();
		
		if (!")".equals(w))
		{
			dk = DatetimeUtil.translate(dk, w);
			if (!")".equals(w))
				throw new ExprParseException("')'", w);
		}
		
		Calendar cale = Calendar.getInstance();
		cale.setTime(dk);

		//Map<List<String>, BigDecimal[]> dv = owner.resolveSymbol(w, lex, t, base);		
   		Map<List<String>, BigDecimal[]> b = new HashMap<List<String>, BigDecimal[]>();
   		b.put(null, owner.val(String.valueOf(cale.get(Calendar.MONTH) + 1)));
		
		return b;
	}
}
